package com.example.beat_x3.Fragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.viewpager2.widget.ViewPager2;

import com.example.beat_x3.Adapter.PlaceScheduleAdapter;
import com.example.beat_x3.Client.GetResult;
import com.example.beat_x3.R;

import java.text.SimpleDateFormat;
import java.util.Date;

import Entity.Inf;
import Entity.PlaceSchedule;

public class PlaceScheduleFragment extends MyFragment {
    public static ViewPager2 viewPager;
    public static PlaceScheduleAdapter placeScheduleAdapter;
    private TextView tvTime;
    private static SimpleDateFormat sdf;
    private String placeNumber;
    private String placeImageSrc;
    private final Handler myHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                tvTime.setText(sdf.format(new Date()));
            } else if (msg.what == 2) {
                placeScheduleAdapter.placeNumber=placeNumber;
                placeScheduleAdapter.placeImageSrc=placeImageSrc;
                placeScheduleAdapter.refresh();
            }
        }
    };

    @SuppressLint("SimpleDateFormat")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_placeschedule, null);
        viewPager = view.findViewById(R.id.re_viewpager);
        viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        placeScheduleAdapter = new PlaceScheduleAdapter(placeNumber, placeImageSrc);
        viewPager.setAdapter(placeScheduleAdapter);
        view.findViewById(R.id.reservation_backbutton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Inf.placeSchedule_in = false;
                changeFragment(Inf.venueFragment);
            }
        });
        tvTime = view.findViewById(R.id.sche_textview);
        sdf = new SimpleDateFormat("HH:mm:ss");
        tvTime.setText(sdf.format(new Date()));
        new TimeThread().start();
        return view;
    }

    public void refresh(String placeNumber, String placeImageSrc) {
        this.placeImageSrc = placeImageSrc;
        this.placeNumber = placeNumber;
        new placeScheduleThread().start();
    }

    class placeScheduleThread extends Thread {
        @Override
        public void run() {
            PlaceSchedule placeSchedule = GetResult.QueryPlaceSchedule(placeNumber);
            if (placeSchedule != null) {
                placeScheduleAdapter.placeSchedule=placeSchedule;
                myHandler.sendEmptyMessage(2);
            }
        }
    }

    class TimeThread extends Thread {
        @Override
        public void run() {
            while (true) {
                myHandler.sendEmptyMessage(1);
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                }
            }
        }
    }
}
